package cn.lopr.im.android.mvp.view.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import cn.lopr.im.android.R;
import cn.lopr.im.android.bean.ArticleCommentVo;
import cn.lopr.im.android.bean.PageInfo;
import de.hdodenhof.circleimageview.CircleImageView;

public class CommentDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_CONTENT = 0;
    private static final int TYPE_FOOTER = 1;
    private static final int TYPE_END = 2;
    private final Context context;
    private CommentDetailAdapter.OnRecyclerViewItemClickListener mOnItemClickListener;

    private PageInfo<ArticleCommentVo> articleCommentVoPageInfo;

    private TextView tvLoadMore;
    private RelativeLayout rlLoad;
    private TextView tvOverLoad;
    private String currentPlateId;

    public CommentDetailAdapter(Context context, PageInfo<ArticleCommentVo> articleCommentVoPageInfo) {
        this.context = context;
        this.articleCommentVoPageInfo = articleCommentVoPageInfo;
    }

    public void refresh(PageInfo<ArticleCommentVo> articleCommentVoPageInfo, String currentPlateId) {
        // 判断是否切换了板块， 如果切换了要清空数据
        if (articleCommentVoPageInfo.isIsFirstPage()) {
            this.articleCommentVoPageInfo.setList(articleCommentVoPageInfo.getList());
        } else if (TextUtils.isEmpty(this.currentPlateId) || this.currentPlateId.equals(currentPlateId)) {
            this.articleCommentVoPageInfo.getList().addAll(articleCommentVoPageInfo.getList());
        } else {
            this.articleCommentVoPageInfo.setList(articleCommentVoPageInfo.getList());
        }

        this.articleCommentVoPageInfo.setIsLastPage(articleCommentVoPageInfo.isIsLastPage());
        notifyDataSetChanged();
        this.currentPlateId = currentPlateId;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_CONTENT) {
            return new ContentViewHolder(LayoutInflater.from(parent.getContext()).inflate
                    (R.layout.list_item_community_article, parent, false));
        } else if (viewType == TYPE_FOOTER || viewType == TYPE_END) {
            // 加载进度条的布局
            return new FooterViewHolder(LayoutInflater.from(parent.getContext()).inflate
                    (R.layout.list_item_footer_load_more, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        if (type == TYPE_CONTENT) {
            initContent((ContentViewHolder) holder, position);

        } else if (type == TYPE_END) {
            tvOverLoad = ((FooterViewHolder) holder).tvOverLoad;
            rlLoad = ((FooterViewHolder) holder).rlLoad;
            tvLoadMore = ((FooterViewHolder) holder).tvLoadMore;
            tvOverLoad.setVisibility(View.VISIBLE);
            rlLoad.setVisibility(View.GONE);
            tvLoadMore.setVisibility(View.GONE);

        } else if (type == TYPE_FOOTER) {
            tvOverLoad = ((FooterViewHolder) holder).tvOverLoad;
            rlLoad = ((FooterViewHolder) holder).rlLoad;
            tvLoadMore = ((FooterViewHolder) holder).tvLoadMore;

        }
    }


    @SuppressLint("SetTextI18n")
    void initContent(ContentViewHolder holder, int position) {
        ArticleCommentVo articleVo = this.articleCommentVoPageInfo.getList().get(position);
        if (mOnItemClickListener != null) {

        }


    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int section);

        void onItemLongClick(View view, int section);
    }

    public void onRecyclerViewItemClickListener(CommentDetailAdapter.OnRecyclerViewItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    /**
     * 获取数据集加上一个footer的数量
     */
    @Override
    public int getItemCount() {
        if (articleCommentVoPageInfo == null || articleCommentVoPageInfo.getList() == null) {
            return 0;
        }
        return articleCommentVoPageInfo.getList().size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            if (this.articleCommentVoPageInfo.isIsLastPage()) {
                return TYPE_END;
            }
            return TYPE_FOOTER;
        } else {
            return TYPE_CONTENT;
        }
    }

    /**
     * 获取数据集的大小
     */
    public int getListSize() {
        return articleCommentVoPageInfo.getList().size();
    }


    /**
     * 显示正在加载的进度条，滑动到底部时，调用该方法，上拉就显示进度条，隐藏"上拉加载更多"
     */
    public void showLoading() {
        if (this.articleCommentVoPageInfo.isIsLastPage()) {
            return;
        }

        if (rlLoad != null && tvLoadMore != null) {
            rlLoad.setVisibility(View.VISIBLE);
            tvLoadMore.setVisibility(View.GONE);
        }
    }

    /**
     * 显示上拉加载的文字，当数据加载完毕，调用该方法，隐藏进度条，显示“上拉加载更多”
     */
    public void showLoadMore() {
        if (this.articleCommentVoPageInfo.isIsLastPage()) {
            //tvOverLoad.setVisibility(View.VISIBLE);
            rlLoad.setVisibility(View.GONE);
            tvLoadMore.setVisibility(View.GONE);
            return;
        }
        if (rlLoad != null && tvLoadMore != null) {
            rlLoad.setVisibility(View.GONE);
            tvLoadMore.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 内容的ViewHolder
     */
    public static class ContentViewHolder extends RecyclerView.ViewHolder {
        View mView;
        LinearLayout llPublishCommentUser;
        CircleImageView ivPortrait;
        TextView tvNickname;
        TextView tvCommentFloor;
        TextView tvComment;
        FrameLayout flCommentReply;
        TextView tvCommentReplyUsername1;
        TextView tvCommentReplyContent1;
        TextView tvCommentReplyUsername2;
        TextView tvCommentReplyContent2;
        TextView tvReplyAll;
        TextView tvCommentPublishTime;
        TextView tvCommentReply;
        ImageView ivReplyComment;

        public ContentViewHolder(View view) {
            super(view);
            this.mView = view;

            llPublishCommentUser = (LinearLayout) view.findViewById(R.id.ll_publish_comment_user);
            ivPortrait = (CircleImageView) view.findViewById(R.id.iv_portrait);
            tvNickname = (TextView) view.findViewById(R.id.tv_nickname);
            tvCommentFloor = (TextView) view.findViewById(R.id.tv_comment_floor);
            tvComment = (TextView) view.findViewById(R.id.tv_comment);
            flCommentReply = (FrameLayout) view.findViewById(R.id.fl_comment_reply);
            tvCommentReplyUsername1 = (TextView) view.findViewById(R.id.tv_comment_reply_username_1);
            tvCommentReplyContent1 = (TextView) view.findViewById(R.id.tv_comment_reply_content_1);
            tvCommentReplyUsername2 = (TextView) view.findViewById(R.id.tv_comment_reply_username_2);
            tvCommentReplyContent2 = (TextView) view.findViewById(R.id.tv_comment_reply_content_2);
            tvReplyAll = (TextView) view.findViewById(R.id.tv_reply_all);
            tvCommentPublishTime = (TextView) view.findViewById(R.id.tv_comment_publish_time);
            tvCommentReply = (TextView) view.findViewById(R.id.tv_comment_reply);
            ivReplyComment = (ImageView) view.findViewById(R.id.iv_reply_comment);
        }
    }

    /**
     * footer的ViewHolder
     */
    public static class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView tvLoadMore;
        TextView tvOverLoad;
        RelativeLayout rlLoad;

        public FooterViewHolder(View itemView) {
            super(itemView);
            tvLoadMore = (TextView) itemView.findViewById(R.id.tv_item_footer_load_more);
            tvOverLoad = (TextView) itemView.findViewById(R.id.tv_over_load);
            rlLoad = (RelativeLayout) itemView.findViewById(R.id.rl_load);
        }
    }

}